//////////////////////////////////////////////////
// glSceneNode.cpp
//
// Author: Tarik Belabbas et Adrien Hoffer
//////////////////////////////////////////////////

#include "glSceneNode.h"

using namespace std;

//------------------------------------------------------------------------------
glSceneNode::glSceneNode( unsigned int _nbChildren )
{
	m_nbChildren = _nbChildren;

	m_children.resize( m_nbChildren );

	for ( vector< glSceneSpatial* >::iterator it = m_children.begin();
			it != m_children.end(); ++it )
	{
		*it = NULL;
	}
}

//------------------------------------------------------------------------------
glSceneNode::~glSceneNode()
{
	for ( vector< glSceneSpatial* >::iterator it = m_children.begin();
				it != m_children.end(); ++it )
	{
		if ( *it )
		{
			(*it)->setParent( NULL );
			delete *it;
		}
	}
}

//------------------------------------------------------------------------------
void glSceneNode::updateWorldTransform()
{
	glSceneSpatial::updateWorldTransform();

	for ( vector< glSceneSpatial* >::iterator it = m_children.begin();
				it != m_children.end(); ++it )
	{
		if ( *it )
		{
			(*it)->updateWorldTransform();
		}
	}
}

//------------------------------------------------------------------------------
void glSceneNode::render()
{
	for ( vector< glSceneSpatial* >::iterator it = m_children.begin();
					it != m_children.end(); ++it )
	{
		if ( *it )
		{
			(*it)->render();
		}
	}
}

//------------------------------------------------------------------------------
void glSceneNode::setChild( unsigned int _index, glSceneSpatial* _child )
{
	 // Detach the current child at the specified index
	if ( m_children[ _index ] )
	{
		m_children[ _index ]->setParent( NULL );
	}

	// Set this as the parent of the new child
	if ( _child )
	{
		_child->setParent( this );
	}

	// Set the new child
	m_children[ _index ] = _child;
}
